package engine.newUi
{
	import engine.control.loading.MultLoader;
	import engine.data.ModelLocator;
	import engine.data.ResPath;
	import engine.flag.ResourcesPath;
	import engine.logic.event.LogicEvent;
	import engine.logic.login.LoginLogic;
	import engine.logic.login.PlayCDTimerLogic;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.scene.SceneManager;
	import engine.tip.TipsMainLogic;
	import engine.tip.tipItem.BaseTipItem;
	import engine.ui.lite.BaseUI;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.DataEvent;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.text.TextFieldAutoSize;
	import flash.utils.Timer;
	
	import newx.com.ResManager;
	import newx.com.config.StaticIni;
	import newx.com.control.NxClip;
	import newx.com.control.window.NxWindowEvent;
	
	/**
	 * 活动商城
	 * @author wangzj
	 */	
	public class TreaShoppingLogic
	{
		private static var _self:TreaShoppingLogic;
		
		private var m_TreaShapping:TreaShoppingUI;
		
		private var m_mcArray:Array;                  //group对象
		private var m_mc1:MovieClip;
		private var m_mc2:MovieClip;
		private var m_mc3:MovieClip;
		private var m_mc4:MovieClip;
		
		private var m_BtnClickFun:Array;           //按钮点击函数
		private var m_BtnOverFun:Array;           //按钮点击函数
		private var m_BtnOutFun:Array;           //按钮点击函数
		
		private var m_startShopTime:Array;          //商城开启时间
		private var m_activionTime:Array;           //商城结束时间
		
		private var m_vector:Vector.<String>;     //物品的id name
		private var m_timeAry:Array;
		private var m_shop:Object;
		
		//字体颜色
		private  const COLOR_TABLE	:Array	=	["","#ffffff'>","#1eff00'>","#20BFF8'>","#a335ee'>","#E8912E'>","#e5cc80'>","#ffff00'>"];
		
		public function TreaShoppingLogic()
		{
		}
		
		public static function getInstance():TreaShoppingLogic
		{
			if(_self == null)
			{
				_self = new TreaShoppingLogic();
				
			}
			return _self;
		}
		
		/**
		 * 初始化界面
		 * 初始换所有的数据
		 */		
		public function Init():void
		{
			m_TreaShapping = TreaShoppingUI.getInstance();
		}
		
		/**
		 * 显示界面
		 * 
		 */		
		public function BeforeShow():void
		{
			ModelLocator.getInstance().addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_ACTIVITY_SHOP_LIST),activityShop);
			
			ModelLocator.getInstance().addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_SENDSERVERTIME),getServerTime);
			
			UISet();
			
			addEvent();        //监听事件
		}
		
		/**
		 * 关闭界面
		 * 
		 */		
		public function AfterHide():void
		{
			removeEvent();
			m_BtnClickFun = null;
			m_mcArray = null;
			m_mc1 = null;
			m_mc2 = null;
			m_mc3 = null;
			m_mc4 = null;
			
			ModelLocator.getInstance().removeEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_ACTIVITY_SHOP_LIST),activityShop);
			ModelLocator.getInstance().removeEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_SENDSERVERTIME),getServerTime);
		}
		/**
		 * 监听事件
		 * 
		 */		
		private function addEvent():void
		{
			
			if(!SceneManager._itemIconLoad)
			{
				ModelLocator.getInstance().addEventListener(LogicEvent.ITEMICONLOADED,loaded);
				function loaded():void
				{
					var DP:DisplayObject ;
					var mc:MovieClip;
					var nxClip:NxClip;
					for( var i:int = 0 ; i < 4 ; i++){
						if(!m_vector)continue;
						var obj:Object= StaticIni.getIniObj(m_vector[i].toString());
						DP = ResManager.newResClass(ResourcesPath.getPropsURL( obj.Photo ));
						if( !DP )continue;
						if( !m_mcArray )continue;
						mc = m_mcArray[i];
						nxClip = mc.getChildByName( "img1" ) as NxClip;
						nxClip.source = DP;
						ModelLocator.getInstance().removeEventListener(LogicEvent.ITEMICONLOADED,loaded)
						
					}
				} 
			}
			
			m_TreaShapping.MoneyBtn.addEventListener(MouseEvent.CLICK , OnTreaShopClick);
			m_TreaShapping.windowBG.addEventListener(NxWindowEvent.WINDOW_CLOSE,onClickCloseHandle);
			for( var i:int = 0; i < 4 ; i++ ){
				m_mcArray[i].btn.addEventListener(MouseEvent.CLICK , m_BtnClickFun[i]);
				(m_mcArray[i] as MovieClip).getChildByName("img01").addEventListener(MouseEvent.ROLL_OVER , m_BtnOverFun[i]);
				(m_mcArray[i] as MovieClip).getChildByName("img01").addEventListener(MouseEvent.ROLL_OUT , onItemListOut);
			}
		}
		
		/**
		 * 移除监听事件
		 * 
		 */		
		private function removeEvent():void
		{
			m_TreaShapping.MoneyBtn.removeEventListener(MouseEvent.CLICK , OnTreaShopClick);
			m_TreaShapping.windowBG.removeEventListener(NxWindowEvent.WINDOW_CLOSE,onClickCloseHandle);
			for( var i:int = 0; i < 4 ; i++ ){
				m_mcArray[i].btn.removeEventListener(MouseEvent.CLICK , m_BtnClickFun[i]);
				(m_mcArray[i].getChildByName( "img01" ) as DisplayObject).removeEventListener(MouseEvent.ROLL_OVER , m_BtnOverFun[i]);
				(m_mcArray[i].getChildByName( "img01" ) as DisplayObject).removeEventListener(MouseEvent.ROLL_OUT , onItemListOut);
			}
		}
		
		/**
		 * 初始化界面 
		 * 
		 */		
		private function UISet():void
		{
			m_BtnClickFun = null;
			m_mcArray = null;
			m_mc1 = null;
			m_mc2 = null;
			m_mc3 = null;
			m_mc4 = null;
			m_vector = null;
			
			m_mcArray = [m_mc1,m_mc2,m_mc3,m_mc4];
			m_BtnClickFun = [onItemListClick0,
				onItemListClick1,
				onItemListClick2,
				onItemListClick3];
			m_BtnOverFun = [onItemListOver0,
				onItemListOver1,
				onItemListOver2,
				onItemListOver3];
			
			
			var mc:MovieClip;
			for( var i:int = 0; i < 4 ; i++ ){
				mc = m_TreaShapping.ItemList.getItem(0,i) as MovieClip;
				m_mcArray[i] =  mc;
			}
			
			m_TreaShapping.layout = BaseUI.CENTER;
			m_TreaShapping.ShoppintText.htmlText = StaticIni.getSwfLangStr("UIString_1000686");
			
			MultLoader.getInstance().load( [ResPath.SWF + "tresureShopping.swf"] , true , gameShow );
			
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_OPEN_ACTIVITY_SHOP_UI);
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_QUERYSERVERTIME);
		}
		
		private function gameShow():void
		{
			m_TreaShapping.backGround.source = MultLoader.getInstance().getSourceByUrl( ResPath.SWF + "tresureShopping.swf" );
		}
		
		/**
		 * 获得数据
		 * @param event
		 * 1 -- 商城开启时间
		 * 2 -- 商城结束时间
		 * 3 道具id
		 * 4 道具货币类型
		 * 5 道具价格
		 * 6 道具限购状态
		 * 7 道具限购个数
		 * 8 道具总个数
		 */		
		private function activityShop( event:CustomMsgEvent ) : void
		{
			var shopList:Array = event.pList;
			var p:int ;
			m_timeAry = [];
			var startString:String = shopList[p++];
			var endString:String   = shopList[p++];
			m_timeAry[0] = startString.substring( 0 , 4 );
			m_timeAry[1] = startString.substring( 4 , 6 );
			m_timeAry[2] = startString.substr( 6 , 2 );
			m_timeAry[3] = endString.substring( 0 , 4 );
			m_timeAry[4] = endString.substring( 4 , 6 );
			m_timeAry[5] = endString.substr( 6 , 2 );
			
			var data:Object;
			m_shop = {};
			
			for( var i:int = 0; i < 4 ; i++ )
			{
				data = {};
				data["id"]         = shopList[p++];
				data["MoneyFlag"] = shopList[p++];
				data["price"]      = shopList[p++];
				data["stutas"]     = shopList[p++];
				data["num"]        = shopList[p++];
				data["total"]      = shopList[p++];
				m_shop[ data["id"] ]  = data;
				
			}
			
			m_TreaShapping.activionLabel.htmlText = StaticIni.getSwfLangStrVar("UIString_1000679" , m_timeAry );
			
			shopMassage( m_shop );
			
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_QUERYSERVERTIME);
		}
		/**
		 * 时间 
		 * @param event
		 * 
		 */		
		private function getServerTime( event:CustomMsgEvent ) : void
		{
			var shopList:Array = event.pList;
			var lastTS:Array = [];
			if( m_TreaShapping.created ){
				var data0:Date = new Date( m_timeAry[3],m_timeAry[4],m_timeAry[5] );
				var lastT:int = (data0.time - shopList[0]*1000)/1000;
				lastTS[0] = int(lastT/(24*3600));
				lastTS[1] = int((lastT - int(lastTS[0]*(24*3600)))/3600);
				var time:Array =( PlayCDTimerLogic.getInstance().getTimerStr( lastT  )as String ).split(":");
				lastTS[2] = time[0];
				m_TreaShapping.lastTimeLab.htmlText   = StaticIni.getSwfLangStrVar("UIString_1000678", lastTS );
			}
		}
		
		/****************点击时间*******************/
		
		/**
		 * 充值按钮
		 * @param event
		 * //待还原
		 */		
		private function OnTreaShopClick( event:MouseEvent ) : void
		{
			/*var model : ModelLocator = ModelLocator.getInstance();
			//是联运游戏,不是本地游戏
			var str:String = model.mainIni.getContent("ChongZhi","url");
			if( "false" == model.mainIni.getContent("ally","is") )
			{//蜗牛网运营
				str = str +　"&account="+ModelLocator.getInstance().player.sAccount+
					"&serverid="+LoginLogic.ServerID;
			}
			
			var url:URLRequest = new URLRequest(str);
			navigateToURL(url,"_blank");*/
		}
		
		private function onClickCloseHandle( event:NxWindowEvent ):void
		{
			m_TreaShapping.showOrHide();
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_CLOSE_ACTIVITY_SHOP_UI);
		}
		
		/**
		 * 点击list物品按钮 
		 * @param event
		 * 
		 */		
		private function onItemListClick0( event:MouseEvent ) : void
		{
			if( !m_vector )return;
			sendItemId(m_vector[0]);
		}
		private function onItemListClick1( event:MouseEvent ) : void
		{
			if( !m_vector )return;
			sendItemId(m_vector[1]);
		}
		private function onItemListClick2( event:MouseEvent ) : void
		{
			if( !m_vector )return;
			sendItemId(m_vector[2]);
		}
		private function onItemListClick3( event:MouseEvent ) : void
		{
			if( !m_vector )return;
			sendItemId(m_vector[3]);
		}
		
		private function onItemListOver0( event:MouseEvent ) : void
		{
			if( !m_vector )return;
			showTips(m_vector[0]);
		}
		private function onItemListOver1( event:MouseEvent ) : void
		{
			if( !m_vector )return;
			showTips(m_vector[1]);
		}
		private function onItemListOver2( event:MouseEvent ) : void
		{
			if( !m_vector )return;
			showTips(m_vector[2]);
		}
		private function onItemListOver3( event:MouseEvent ) : void
		{
			if( !m_vector )return;
			showTips(m_vector[3]);
		}
		
		private function showTips( id:String ) : void
		{
			var obb:Object=TipsMainLogic.instance.getStaticObjWithID( id );
			obb["SellValue"]="<font color='#ffd100'>" + m_shop[id].price+ "</font>";
			obb["_type"]= "1";
			obb["_kind"]= m_shop[id].MoneyFlag;
			TipsMainLogic.instance.showMain(obb,true);
		}
		
		
		private function onItemListOut( event:MouseEvent ) : void
		{
			TipsMainLogic.instance.hide();
		}
		
		/***************数据填充******************/
		/**
		 * 向服务端发送购买物品id 
		 * @param itemId
		 * 
		 */		
		private function sendItemId( itemId:String ) :void
		{
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_ACTIVITY_SHOP_BUY_ITEM ,
				int(itemId) );
		}
		
		/**
		 * 物品显示信息 
		 * @param shop
		 */		
		private function shopMassage( shop : Object ) : void
		{
			var p:int;
			var mc:MovieClip;
			var name:String;
			var nxClip:NxClip;
			var disClip:DisplayObject;
			m_vector = new Vector.<String>();
			for each( var i:Object in shop )
			{
				mc = m_mcArray[p++] as MovieClip;
				disClip = mc.getChildByName( "img1" );
				var obj:Object= StaticIni.getIniObj( i.id );
				if( !disClip ){
					nxClip = new NxClip;
					nxClip.source = ResManager.newResClass(ResourcesPath.getPropsURL( obj.Photo ));
					nxClip.x = mc["img01"].x;
					nxClip.y = mc["img01"].y;
					nxClip.name = "img1";
					nxClip.mouseEnabled = false;
					var index:int = mc.getChildIndex( mc["img01"] );
					mc.addChildAt( nxClip , index);
					m_vector.push( i.id );
				}else{
					nxClip = disClip as NxClip;
					nxClip.source = null;
					nxClip.source = ResManager.newResClass(ResourcesPath.getPropsURL( obj.Photo ));
					m_vector.push( i.id );
				}
				
				name = StaticIni.getSwfLangStr( i.id.toString() );
				mc.txt_0.htmlText ="<font color ='"+COLOR_TABLE[obj.Color]  +name+"</font>" ;
				mc.pc.visible = i.stutas != 1 ? true : false;
				(mc.pc as MovieClip).mouseEnabled = false;
				
				if( i.total == 0 && i.stutas != 1)
				{
					mc.btn.visible = false;
				}
				
				if( i.stutas == 1 ){
					mc.txt_1.htmlText = "      "+StaticIni.getSwfLangStr("UIString_1000674");
				}else if( i.stutas == 2){
					mc.txt_1.htmlText = StaticIni.getSwfLangStrVar("UIString_1000676",[ i.num ] );
				}else if( i.stutas == 3 ){
					mc.txt_1.htmlText = StaticIni.getSwfLangStrVar("UIString_1000677",[ i.num ] );
				}else if( i.stutas == 4 ){
					mc.txt_1.htmlText = StaticIni.getSwfLangStrVar("UIString_1000675",[ i.num ] );
				}
				
				mc.txt_1.autoSize = TextFieldAutoSize.CENTER;
			}
		}
		
	}
}